package com.crm.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.crm.dao.ConfigDAO;
import com.crm.po.Config;
import com.crm.util.RequestEx;

public class ConfigService {
	
	private static Logger logger = Logger.getLogger(ConfigService.class);
	
	public List<Config> getConList(){
		String Hql = " from Config t where 1=1 ";
		ConfigDAO dao = new ConfigDAO();
	    List<Config> list = dao.configList(Hql);
	    return list;
	}
	
	public Config getConfig(HttpServletRequest request){
		Config config = null;
		try{
			String key = RequestEx.getStringTrim(request.getParameter("key"));
			if(key.length() > 0){
				String hql = "from Config T WHERE T.CKey = '" + key + "'";
				ConfigDAO dao = new ConfigDAO();
				Object obj = dao.getUniqueResult(hql);
				if(obj != null){
					config = (Config) obj;
				}
			}			
		}catch(Exception e){
			logger.error("select config fail:", e);
			return null;
		}
	    return config;
	}
	
	public Config getConfigByKey(String key){
		Config config = null;
		try{
			if(key.length() > 0){
				String hql = "from Config T WHERE T.CKey = '" + key + "'";
				ConfigDAO dao = new ConfigDAO();
				Object obj = dao.getUniqueResult(hql);
				if(obj != null){
					config = (Config) obj;
				}
			}			
		}catch(Exception e){
			logger.error("select taxrate fail:", e);
			return null;
		}
	    return config;
	}
	
	public boolean exeSystemConfig(HttpServletRequest request){
		int result = 0;
		try{
			String conid = RequestEx.getStringTrim(request.getParameter("conid"));
			String CValue = RequestEx.getStringTrim(request.getParameter("CValue"));
			if(conid.length() > 0){
				String hql = "UPDATE Config T SET T.CValue = '" + CValue + "' where T.conid = '" + conid + "'";
				ConfigDAO dao = new ConfigDAO();
				result = dao.executeHql(hql);
			}			
		}catch(Exception e){
			logger.error("update config fail:", e);
			return false;
		}
	    return result > 0 ? true: false;
	}
	
	public static void main(String[] args){

	}
	
}
